/* global App */

(function() {
    
    var Birthdays = {
        
        loaded: false,

        init: function() {
            this.setup();
        },

        setup: function() {
            var self = this;

            $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
                if ($(e.target).attr('href') == "#birthdays") {
                    self.load();
                }
            });
        },

        load: function() {
            if (this.loaded) {
                return;
            }

            var self = this;
            var url = $("#birthdays-data-url").val();

            $("#birthdays").html("Loading ...");

            $.ajax({
                url: url,
                type: 'post',
                success: function(response) {
                    self.loaded = true;

                    $("#birthdays").html(response);
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
        }
    };
    
    App.register('Birthdays', Birthdays);
})();